package com.alibaba.luohua.product.controller;

import com.alibaba.luohua.common.core.dto.request.ArticleQueryObject;
import com.alibaba.luohua.common.core.dto.respone.ArticleDTO;
import com.alibaba.luohua.common.core.dto.respone.ArticleRspDTO;
import com.alibaba.luohua.common.core.web.controller.BaseController;
import com.alibaba.luohua.common.core.web.domain.PageResult;
import com.alibaba.luohua.common.core.web.domain.SingleResult;
import com.alibaba.luohua.product.service.ArticleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 物品模块控制层
 *
 * @author 罗华
 * @date 2022/3/15
 */
@RestController
@RequestMapping("/article")
public class ArticleController extends BaseController {


    @Autowired
    private ArticleService articleService;


    /**
     * 查询物品详情
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public SingleResult<ArticleRspDTO> get(@RequestParam("id") Long id) {
        return SingleResult.success(articleService.get(id));
    }

    /**
     * 分页查询物品列表
     *
     * @param pageDTO
     * @return
     */
    @PostMapping("/pageInfo")
    public PageResult<ArticleDTO> pageInfo(@RequestBody ArticleQueryObject pageDTO) {

        PageHelper.startPage(pageDTO.getPageNum(), pageDTO.getPageSize());
        List<ArticleDTO> list = articleService.queryForList(pageDTO);
        PageInfo<ArticleDTO> pageInfo = PageInfo.of(list);
        return PageResult.success(list,
                pageInfo.getTotal(),
                pageDTO.getPageNum(),
                pageDTO.getPageSize());
    }


}
